package com.serkol.ams.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.ams.bean.MyPage;
import com.serkol.ams.bean.MyPageOld;
import com.serkol.ams.entity.CloudOrderMaster;
import com.serkol.ams.entity.LoginLog;
import com.serkol.ams.entity.OrderMaster;
import com.serkol.ams.entity.UsersAddress;
import com.serkol.ams.mapper.CloudOrderMasterMapper;
import com.serkol.ams.modal.SqlFeildModal;
import com.serkol.ams.service.*;
import com.serkol.constant.AuthConstant;
import com.serkol.handler.MyLambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author pingan
* @description 针对表【cloud_order_master】的数据库操作Service实现
* @createDate 2022-10-23 13:03:51
*/
@Service
public class CloudOrderMasterServiceImpl extends ServiceImpl<CloudOrderMasterMapper, CloudOrderMaster>
    implements CloudOrderMasterService{
    @Autowired
    private CloudOrderDetailService detailService;

    @Autowired
    private UsersAddressService usersAddressService;
    @Autowired
    private ShopService shopService;
    @Override
    public MyPage<Map<String, Object>> findByPageList(int pageSize, int current, String consignerName, String userName, String sn, String partySn) {
        QueryWrapper<CloudOrderMaster> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(SqlFeildModal.getOrderCloudFeild());

        if (StringUtils.isNotEmpty(consignerName)) {
            queryWrapper.lambda().inSql(CloudOrderMaster::getConsignerId, SqlFeildModal.getAuthNameLike(consignerName));
        }

        if (StringUtils.isNotEmpty(userName)) {
            queryWrapper.lambda().inSql(CloudOrderMaster::getUserId, SqlFeildModal.getAuthNameLike(userName));
        }
        if (StringUtils.isNotEmpty(sn)) {
            queryWrapper.lambda().eq(CloudOrderMaster::getSn, sn);
        }
        if (StringUtils.isNotEmpty(partySn)) {
            queryWrapper.lambda().eq(CloudOrderMaster::getPartySn, partySn);
        }
        queryWrapper.orderByDesc("ID");
        Page<Map<String, Object>> page = new Page<>(current, pageSize);
        Page<Map<String, Object>> mapPage = this.baseMapper.selectMapsPage(page, queryWrapper);
        return MyPage.parsePage(mapPage);
    }

    @Override
    public List<Map<String, Object>> findByMyList(int status) {
        LoginLog loginLog = AuthConstant.getLoginLog();
        MyLambdaQueryWrapper<CloudOrderMaster> lambdaQueryWrapper = new MyLambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CloudOrderMaster::getUserId, loginLog.getUserId());
        lambdaQueryWrapper.orderByDesc(CloudOrderMaster::getId);
        if (status > 0) {
            lambdaQueryWrapper.eq(CloudOrderMaster::getStatus, status);
        }
        List<CloudOrderMaster> list = this.list(lambdaQueryWrapper);
        List<Map<String, Object>> maps = new ArrayList<>();
        for (CloudOrderMaster orderMaster : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("order", orderMaster);
            List<Map<String, Object>> byOrderDetailList = detailService.findByOrderDetailList(orderMaster.getId());
            map.put("details", byOrderDetailList);
            maps.add(map);

        }


        return maps;
    }

    @Override
    public Map<String, Object> info(long id) {
        Map<String, Object> map = new HashMap<>();
        CloudOrderMaster order = this.getById(id);

            UsersAddress address = usersAddressService.getById(order.getAddressId());
            map.put("address", address);

        List<Map<String, Object>> details = detailService.findByOrderDetailList(order.getId());


        map.put("order", order);

        map.put("details", details);
        return map;
    }

}




